/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-11-10
 * Time: 16:24
 */
public class Solution {
    public int[] sortArrayByParity(int[] nums) {
        int left = 0;
        int right = nums.length-1;
        while (left < right) {
            while (left < right && nums[right]%2 != 0) {
                right--;
            }
            while (left < right &&nums[left]%2 == 0) {
                left++;
            }
            if (left < right) {
                swap(nums,left++,right--);
            }
        }

        return nums;
    }

    //交换方法
    private void swap(int[] arr,int i,int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}
